package com.lhwb.escsvinput.service;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;

import com.lhwb.escsvinput.esMapper.TmdbMapper;
import com.lhwb.escsvinput.vo.TmdbIndex;

public class TmdbReadExcelListener implements ReadListener<TmdbIndex> {

    public static TmdbMapper tmdbEntityRepository = null;

    public static TmdbMapper getEsTemplate() {
        // 双重判空获取
        if (tmdbEntityRepository == null) {
            synchronized (TmdbReadExcelListener.class) {
                if (tmdbEntityRepository == null) {
                    tmdbEntityRepository = ApplicationContextProvider.getBean(TmdbMapper.class);
                }
            }
        }
        return tmdbEntityRepository;
    }

    @Override
    public void invoke(TmdbIndex tmdbIndex, AnalysisContext analysisContext) {
        // 插入到数据库
        TmdbMapper mapper = getEsTemplate();
        try {
            mapper.insert(tmdbIndex);
        }catch (Exception e) {
            System.out.println("插入失败"+ JSON.toJSONString(tmdbIndex));
            e.printStackTrace();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
